﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;

namespace FacebookApp
{
    public class PublisherService
    {
        //readonly static DataClassesDataContext dt = new DataClassesDataContext();

        #region[Publisher_GetbyAll]
        public static List<Publisher> Publisher_GetByAll()
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    var da = (from pu in dt.Publishers select pu).ToList();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
        #region[Publisher_GetbyId]
        public static Publisher Publisher_GetById(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    var da = (from pu in dt.Publishers where pu.Id == id select pu).First();

                    return da;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
        #region[Publisher_GetbyTop]
        public static List<Publisher> Publisher_GetByTop(string Top, string Where, string Order)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    string query = "Select * from [Publisher]";
                    if (!Top.Equals(""))
                        query = " Select top " + Top + " * from [Publisher] ";
                    if (!Where.Equals(""))
                        query += " where " + Where + " ";
                    if (!Order.Equals(""))
                        query += " order by " + Order;

                    IEnumerable<Publisher> results = dt.ExecuteQuery<Publisher>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
        #endregion
        #region[Publisher_Insert]
        public static void Publisher_Insert(PublisherInfo Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    Publisher ob = new Publisher();
                    ob.Name = Data.Name;
                    ob.Decription = Data.Decription;
                    dt.Publishers.InsertOnSubmit(ob);
                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }
        #endregion
        #region[Publisher_Update]
        public static void Publisher_Update(PublisherInfo Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data.Id);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Publisher da = dt.Publishers.Single(au => au.Id == id);
                    da.Name = Data.Name;
                    da.Decription = Data.Decription;

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }


        #endregion
        #region[Publisher_Delete]

        public static void Publisher_Delete(string Data)
        {
            try
            {
                using (var dt = new DataClassesDataContext())
                {
                    int id = int.Parse(Data);
                    //Author da = (from au in dt.Authors where au.Id == id select au).First();
                    Publisher da = dt.Publishers.Single(au => au.Id == id);
                    dt.Publishers.DeleteOnSubmit(da);

                    dt.SubmitChanges();
                }
            }
            catch (Exception)
            {

            }
        }

        #endregion
    }

    public class PublisherInfo
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Decription { get; set; }

    }
}